/**
 * 
 * 用户相关方法
 */

const { execSQL } = require("../db/mysql.js");

// 查找用户是否存在
const findUser = username => {
  
  const sql = `select * from users where username='${username}'`;

  return execSQL(sql).then(rows => {
    console.log('rows', rows);
    return rows[0]
  })
}

// 新增用户
const createUser = (userData = {}) => {

  const username = userData.username
  const password = userData.password

  const sql = `
    insert into users (username, password) values ('${username}', '${password}')
  `
  return execSQL(sql).then(insertedResult => {
    console.log('insertedResult', insertedResult);
    return {
      id: insertedResult.insertId
    }
  })
  
}

module.exports = {
  findUser,
  createUser,
}